// pages/messages/list/list.js
const customRequst = require('../../../common/customRequst.js');

Page({

  data: {
    list:[],
    page:1,
    pagesize:15,
    loadAllMsg:false,
  },

  onLoad: function (options) {
    let that = this
    that.onLoadList()
  },

  //加载消息列表
  onLoadList:function(){
    let that = this;
    customRequst.postRequest('api/sysmsg/list', {
      page: that.data.page,
      pagesize: that.data.pagesize,
    }).then(res => {
      if(res.code == 1){
        if(res.data.length <= 0){
          that.setData({
            loadAllMsg:true
          });
          return;
        }
        that.setData({
          list: res.data
        })
      }else{
        wx.showToast({
          title: res.msg,
        })
      }
    
      
    })
  },
  //标记消息已读
  clickMsg(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.ready_status;
    if(status != 2){
      return;
    }
    customRequst.postRequest('/api/sysmsg/read', {
      id: e.currentTarget.dataset.id,
    }).then(res => {
      if (res.code == 1) {
        let msgList = that.data.list;
        for(let i in msgList){
          if(msgList[i].id == id){
            msgList[i].ready_status = 1;
            break;
          }
        }
        that.setData({
          list:msgList
        })
      }
    })
  },

   //滚动=条触底
   scrollSearchList:function(){
    let that = this;
    if(that.data.loadAllMsg){
      return;
    }
    that.setData({
      page:parseInt(that.data.page) + 1
    });
    that.onLoadList();
  }

})